package com.riteny.common.entity;

import com.riteny.common.httpclient.CommonHttpRestfulTemplate;
import net.sf.json.JSONObject;

import java.util.HashMap;

/**
 * @author Riteny
 * 2021/10/22  10:34
 */
public class CommonHttpParamMapNode extends HashMap implements CommonHttpParamNode {

    private CommonHttpParamNode prevNode;

    private CommonHttpRestfulTemplate httpRestfulTemplate;


    public CommonHttpParamMapNode(CommonHttpRestfulTemplate httpRestfulTemplate) {
        this.httpRestfulTemplate = httpRestfulTemplate;
    }

    public CommonHttpParamMapNode addParam(String key, Object value) {

        put(key, value);

        return this;
    }

    public CommonHttpParamMapNode addParams(JSONObject paramJson) {

        putAll(paramJson);

        return this;
    }

    public CommonHttpParamMapNode addMapNode(String key) {

        CommonHttpParamMapNode newNode = new CommonHttpParamMapNode(httpRestfulTemplate);
        newNode.setPrevNode(this);

        put(key, newNode);

        return newNode;
    }

    public CommonHttpParamArrayNode addArrayNode(String key) {

        CommonHttpParamArrayNode newNode = new CommonHttpParamArrayNode(httpRestfulTemplate);
        newNode.setPrevNode(this);

        put(key, newNode);

        return newNode;
    }

    public CommonHttpParamMapNode prevMapNode() {
        return (CommonHttpParamMapNode) prevNode;
    }

    public CommonHttpParamArrayNode prevArrayNode() {
        return (CommonHttpParamArrayNode) prevNode;
    }

    public CommonHttpRestfulTemplate and() {
        return httpRestfulTemplate;
    }

    public void setPrevNode(CommonHttpParamNode prevNode) {
        this.prevNode = prevNode;
    }

    public void setHttpRestfulTemplate(CommonHttpRestfulTemplate httpRestfulTemplate) {
        this.httpRestfulTemplate = httpRestfulTemplate;
    }
}
